import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// 创建仓库 实例对象
const store = new Vuex.Store({
  actions: {
    add_num_async (context, n) {
      /*
      context 上下文
      其实 store这个对象
      context.commit()
      */
      setTimeout(() => {
        const num = n
        context.commit('add_num', num)
      }, 2000)
    }
  },
  mutations: {
    add_num (state, n) {
      state.num += n
    },
    reduce_num (state, n) {
      state.num -= n
    }
  },
  state: {
    num: 10,
    items: []
  }
})

export default store
